<template>
	<view>
		<view class="addres-container" @tap="addres">
			<view class="addres-text " v-if="addresinfo==null" >
				添加地址
			</view>
			<view class="addres-text" v-if="addresinfo!=null">
				<view class="">
					{{addresinfo.name}} {{addresinfo.number}}
				</view>
				<view class="addres-area">
					{{addresinfo.area}}{{addresinfo.addres}}
				</view>
				<view class="addres-edit">
					编辑
				</view>
			</view>

		</view>
		<view class="butt">
			
		</view>

		<view class="pay">
			<van-radio-group v-model="radio">
				<van-cell-group>

					<van-cell icon="http://s1.mi.com/m/images/m/pay_zfb2.png" icon-size="50px" title="支付宝" clickable @click="radio = '支付宝'">

						<template #right-icon>
							<van-radio name="支付宝" />
						</template>
					</van-cell>
					<van-cell icon="http://s1.mi.com/m/images/m/pay_wx.png" title="微信支付" clickable @click="radio = '微信支付'">
						<template #right-icon>
							<van-radio name="微信支付" />
						</template>
					</van-cell>
					<van-cell icon="http://s1.mi.com/m/images/m/pay_yl1.png" title="银联支付" clickable @click="radio = '银联支付'">
						<template #right-icon>
							<van-radio name="银联支付" />
						</template>
					</van-cell>
					<van-cell icon="http://s1.mi.com/m/images/m/pay_antinstalment.png" title="花呗分期" clickable @click="radio = '花呗分期'">
						<template #right-icon>
							<van-radio name="花呗分期" />
						</template>
					</van-cell>
					<van-cell icon="http://s1.mi.com/m/images/m/pay_micashcreditinstallment_wap.png" title="信用卡分期" clickable @click="radio = '信用卡分期'">
						<template #right-icon>
							<van-radio name="信用卡分期" />
						</template>
					</van-cell>

				</van-cell-group>
			</van-radio-group>
		</view>


		<van-submit-bar :price="amount*100" button-text="提交订单" @submit="OnAuthentication" />

	</view>
</template>

<script>
	import {
		mapState,
		mapGetters,
		mapMutations
	} from "vuex"
	export default {
		data() {
			return {
				radio: 1,
				checkIsSoterEnrolled: false
			}
		},
		computed: {

			...mapState("addres", ['addresinfo']),

			...mapState("cart", ["cart"]),
			...mapGetters("cart", ["amount", "selectedcount"]),
			...mapMutations("cart", ["emptyCar"])
		},
		methods: {
			OnAuthentication() {
				this.checkIsSoterEnrolledInDeviceFingerPrint()
				if (this.checkIsSoterEnrolled) {
					this.startSoterAuthenticationFingerPrint()
				}else{
					console.log("请输入支付密码")
					}
			},

			checkIsSoterEnrolledInDeviceFingerPrint() {

				uni.checkIsSoterEnrolledInDevice({
					checkAuthMode: 'fingerPrint',
					success: (res) => {
						if (res.isEnrolled) {
							console.log("支持指纹");
							this.checkIsSoterEnrolled = true;
						}

					},
					fail(err) {

						console.log(err);
					},
					complete(res) {

						console.log(res);
					}
				})

			},

			startSoterAuthenticationFingerPrint() {
				uni.startSoterAuthentication({
					requestAuthModes: ['fingerPrint'],
					challenge: '123456',
					authContent: '请用指纹支付',
					success: (res) => {
						console.log(res);
						this.onSubmitt()
					},
					fail(err) {
						console.log(err);
					},
					complete(res) {
						console.log(res);
					}
				})
			},
			addres() {
				uni.navigateTo({
					animationType: 'slide-in-left',
					animationDuration: 300,
					url: "../../components/addres"
				})
			},
			//提交订单
			onSubmitt() {
				//封装
				let data = {
					way: this.radio,
					 addresinfo: this.addresinfo,
					cart: this.cart,
					amount: this.amount
				}

				uni.request({
					url: this.$apiServer + "submitOrder",
					method: "POST",
					data: data,
					success: (res) => {
						uni.showToast({
							title: "支付成功!"
						})
						setTimeout(() => {
							uni.switchTab({
								animationType: 'slide-in-left',
								animationDuration: 300,
								url: "../me/me"
							})
						}, 200)
						this.emptyCar();
						this.selectedcount = 0;
					}
				})
			},
			setbarbadge() {

				uni.hideTabBarRedDot({
					index: 2,
				});
			}
		},
		watch: {
			selectedcount(newValue, oldValue) {

				this.setbarbadge()
			}
		},
		onLoad() {
			console.log(this.addresinfo)
		}
	}
</script>

<style lang="scss" scoped>
	.addres-container {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		padding: 25px;


	}

	.addres-area {
		font-size: 15px;
	}

	.addres-edit {
		position: absolute;
		right: 5px;
		margin-top: -40px;
	}

	.butt {
		right: 0;
		bottom: 0;
		left: 0;
		height: 2px;
		background: -webkit-repeating-linear-gradient(135deg, #ff6c6c 0, #ff6c6c 20%, transparent 0, transparent 25%, #1989fa 0, #1989fa 45%, transparent 0, transparent 50%);
		background: repeating-linear-gradient(-45deg, #ff6c6c 0, #ff6c6c 20%, transparent 0, transparent 25%, #1989fa 0, #1989fa 45%, transparent 0, transparent 50%);
		background-size: 80px;
		content: '';
	}
</style>
